﻿using System;
using System.CodeDom;
using System.ComponentModel.Design.Serialization;

namespace YArchitech.Controls.ImgListView
{
	internal class ImageListViewSerializer : CodeDomSerializer
	{
		public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
		{
			return ((CodeDomSerializer)manager.GetSerializer(typeof(ImageListView).BaseType, typeof(CodeDomSerializer))).Deserialize(manager, codeObject);
		}

		public override object Serialize(IDesignerSerializationManager manager, object value)
		{
			object obj = ((CodeDomSerializer)manager.GetSerializer(typeof(ImageListView).BaseType, typeof(CodeDomSerializer))).Serialize(manager, value);
			if (obj is CodeStatementCollection)
			{
				CodeStatementCollection codeStatementCollection = (CodeStatementCollection)obj;
				CodeExpression codeExpression = base.SerializeToExpression(manager, value);
				if (codeExpression != null && value is ImageListView)
				{
					int num = 0;
					foreach (ImageListView.ImageListViewColumnHeader imageListViewColumnHeader in ((ImageListView)value).Columns)
					{
						if (!(imageListViewColumnHeader.Text == imageListViewColumnHeader.DefaultText) || imageListViewColumnHeader.Width != 100 || imageListViewColumnHeader.DisplayIndex != num || num < 4 != imageListViewColumnHeader.Visible)
						{
							CodeMethodInvokeExpression codeMethodInvokeExpression = new CodeMethodInvokeExpression(codeExpression, "SetColumnHeader", new CodeExpression[]
							{
								new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(ColumnType)), Enum.GetName(typeof(ColumnType), imageListViewColumnHeader.Type)),
								new CodePrimitiveExpression(imageListViewColumnHeader.Text),
								new CodePrimitiveExpression(imageListViewColumnHeader.Width),
								new CodePrimitiveExpression(imageListViewColumnHeader.DisplayIndex),
								new CodePrimitiveExpression(imageListViewColumnHeader.Visible)
							});
							if (imageListViewColumnHeader.Text == imageListViewColumnHeader.DefaultText)
							{
								codeMethodInvokeExpression.Parameters.RemoveAt(1);
							}
							codeStatementCollection.Add(codeMethodInvokeExpression);
						}
						num++;
					}
				}
				return obj;
			}
			return base.Serialize(manager, value);
		}
	}
}
